home *** CD-ROM | disk | FTP | other *** search
/ Interplay's Learn to Program Basic (Review Copy) / Learn to Program Basic Review Copy (Interplay)(June 23, 1998).ISO / pc / ltpbasic / projects / invader.bas < prev    next >
Encoding:
BASIC Source File  |  1998-03-26  |  6.1 KB  |  307 lines

  1. Rem -------------------------
  2. Rem Invader!
  3. Rem -------------------------
  4. CLS
  5. Rem Set up Arrays
  6. Let aliensPerRow = 3
  7. Let numRows = 4
  8. Let numAliens = aliensPerRow * numRows
  9. Dim alienSprite(numAliens)
  10. Dim alienAlive(numAliens)
  11. Dim alienBomb(numAliens)
  12. Dim bombX(numAliens)
  13. Dim bombY(numAliens)
  14. Dim bombDropping(numAliens)
  15. Rem Set the sprites
  16. For a = 1 To numAliens
  17. Let alienSprite(a) = LoadSprite("ufo")
  18. CycleSprite alienSprite(a) speed 10
  19. Let alienBomb(a) = LoadSprite("invbomb")
  20. CycleSprite alienBomb(a) Speed 5
  21. Let alienAlive(a) = TRUE
  22. Let bombDropping(a) = FALSE
  23. Next a
  24. Let gun = LoadSprite("invgun")
  25. Let misl = LoadSprite("invmisl")
  26. CycleSprite misl speed 30
  27. Let boom = LoadSprite("woozy01")
  28. SetSprite boom range 0 to 11
  29. Rem Set up game variables
  30. Let width = 75
  31. Let height = 35
  32. Let gunMoveSpeed = 10
  33. Let top = 0
  34. Let left = 0
  35. Let yDropEachTime = 2
  36. Let gunX = 140
  37. Let gunY = 230
  38. Let mislSpeed = 15
  39. Let mislFired = FALSE
  40. Let bombSpeed = 1
  41. Let PlayerHit = FALSE
  42. Let alienSpeed = 1
  43. Let GameOver = FALSE
  44. Let Lives = 3
  45. Let Score = 0
  46. Let ExitTimer = 0
  47. Let RunGame = TRUE
  48.  
  49. Rem ----
  50. Rem Display instructions
  51. Rem Then run game
  52. Rem ----
  53. Gosub Instructions
  54. Gosub DoAlienGame
  55. Rem Hide all remaining aliens
  56. For a = 1 to numAliens
  57. HideSprite alienSprite(a)
  58. HideSprite alienBomb(a)
  59. Next a
  60. Rem Hide any remaining gun or missle
  61. HideSprite misl
  62. HideSprite gun
  63. Rem Tell user he can play again
  64. Home
  65. TextColor 235
  66. Print
  67. Print "Click RUN to play again!"
  68. End
  69.  
  70. Rem Subroutines
  71.  
  72. Rem ----
  73. Rem Give Instructions
  74. Rem ----
  75. Instructions:
  76. CLS
  77. TextColor 190 'Eye Blue
  78. Print "Invaders!"
  79. Print 
  80. Print "Use the Left and Right arrow keys"
  81. Print "to move your gun, and the Space Bar"
  82. Print "to shoot."
  83. Print 
  84. Print "The aliens get faster and madder"
  85. Print "the better you do, so be careful!"
  86. Print
  87. Print "Press a key when ready to play"
  88. while inkey$ <> ""
  89. wend
  90. while inkey$ = ""
  91. wend
  92. Rem Now set up game screen
  93. Rem using a background
  94. CLS
  95. Background "Earth"
  96. Return
  97.  
  98. Rem ----
  99. Rem Handle alien movements
  100. Rem and call player subroutine
  101. Rem ----
  102. DoAlienGame:
  103. move = 1
  104. while RunGame
  105. Rem 'move' negative to move left
  106. If move > 0 then
  107. move = alienSpeed
  108. else
  109. move = -alienSpeed
  110. Endif
  111. Rem 'going' is true until we bump an edge
  112. going = TRUE
  113. While going
  114. Rem 'aliensLeft' is used to count how many remain
  115. aliensLeft = 0
  116. Rem Subroutine 'Play' handles gun and missles
  117. Gosub Play
  118. Rem Draw all of the aliens
  119. SuspendUpdate
  120. For a = 1 to numAliens
  121. Rem calculate the position of this alien
  122. r = Int((a-1) / numRows)
  123. c = (a-1) mod numRows
  124. y = top + r * height
  125. x = left + c * width
  126. Rem See if alien should drop a bomb
  127. If bombDropping(a) = FALSE Then
  128. If alienAlive(a) AND Random(1,100) < 5 Then 
  129. bombX(a) = x + 27
  130. bombY(a) = y + 33
  131. bombDropping(a) = TRUE
  132. Endif
  133. Endif
  134. Rem update a dropping bomb
  135. If bombDropping(a) Then 
  136. SetSprite alienBomb(a) to bombX(a),bombY(a)
  137. bombY(a) = bombY(a) + bombSpeed
  138. If bombY(a) > 240 Then 
  139. HideSprite alienBomb(a)
  140. bombDropping(a) = FALSE
  141. Endif
  142. Rem See if bomb hit player
  143. If SpriteHitSprite(alienBomb(a), gun) then
  144. Rem Player Hit
  145. HideSprite alienBomb(a)
  146. bombDropping(a) = FALSE
  147. If PlayerHit = FALSE Then
  148. playerHit = TRUE
  149. Sound "EXP_BIG"
  150. EndIf
  151. Endif
  152. Endif
  153.  
  154. Rem Move the alien
  155. If alienAlive(a) Then
  156. aliensLeft = aliensLeft + 1
  157. Rem See if we bump the edge and need to reverse direction
  158. If move > 0 And x > 250 Then
  159. If spriteHitPoint(alienSprite(a),319,y+20) Then going = FALSE
  160. EndIf
  161. If move < 0 and x < 0 Then Going = FALSE
  162.  
  163. Rem see if we collide with gun
  164. If y > 210 Then 
  165. Rem Kill Player
  166. PlayerHit = TRUE
  167. Rem Reset aliens
  168. for t = 1 to numAliens
  169. alienAlive(t) = FALSE
  170. bombDropping(t) = FALSE
  171. Next t
  172. EndIf
  173.  
  174. Rem Put alien at right spot on screen
  175. SetSprite alienSprite(a) to x,y
  176.  
  177. Rem See if missle hits this alien
  178. If mislFired Then
  179. If SpriteHitSprite(alienSprite(a), misl) then
  180. Rem Alien hit
  181. HideSprite alienSprite(a)
  182. HideSprite misl
  183. Sound "EXP_FAR"
  184. mislFired = FALSE
  185. alienAlive(a) = FALSE
  186. Rem Score
  187. score = score + 50 * yDropEachTime
  188. Rem every alien killed makes game go a little faster
  189. alienSpeed = alienSpeed + .3
  190. Endif
  191. Endif
  192. Endif
  193. Next a
  194. ResumeUpdate
  195. Rem move the row of aliens
  196. left = left + move
  197. Rem If all aliens gone, end loop
  198. If aliensLeft = 0 then going = FALSE
  199. Wend ' (going)
  200. Rem now move the opposite direction
  201. move = -move
  202. going = TRUE
  203. If aliensLeft Then
  204. Rem drop down a line, and drop bombs a little faster
  205. top = top + yDropEachTime
  206. bombSpeed = bombSpeed + .2
  207. Else
  208. Rem Reset the aliens for next round
  209. For a = 1 to numAliens
  210. alienAlive(a) = TRUE
  211. Next a
  212. Rem put them back at the top
  213. top = 0
  214. Rem Make them drop a little faster in next round
  215. yDropEachTime = yDropEachTime + 1
  216. Rem make a sound to announce new round
  217. If PlayerHit = FALSE Then Sound "BOING1"
  218. Endif
  219. Wend
  220. Return
  221.  
  222. Rem ----
  223. Rem Handle Player
  224. Rem ----
  225. Play:
  226. Rem handle gun with arrow keys
  227. if KeyDown("CurLeft") Then 
  228. gunX = gunX - gunMoveSpeed
  229. if gunX < 0 then gunX = 0
  230. EndIf
  231. if KeyDown("CurRight") Then
  232. gunX = gunX + gunMoveSpeed
  233. if gunX > 300 then gunX = 300
  234. Endif
  235. Rem see if we are firing missle
  236. if mislFired = FALSE Then
  237. if KeyDown("space") Then 
  238. mislY = 210
  239. mislX = gunX + 15
  240. mislFired = TRUE
  241. Endif
  242. Endif
  243.  
  244. Rem handle player getting hit
  245. if PlayerHit = TRUE Then
  246. Rem just got hit
  247. If GetSpriteFrame(boom) = 0 Then
  248. HideSprite gun
  249. SetSprite boom to gunx,guny - 20
  250. Rem run through the explosion only once
  251. CycleSprite boom speed 10 Once
  252. Rem player loses one of his lives
  253. Lives = Lives - 1
  254. If Lives = 0 Then 
  255. GameOver = TRUE
  256. Rem game is over, but let's not end until a timer expires
  257. ExitTimer = timer()
  258. Rem put Game Over text on the screen
  259. DrawText "@14,bold,RED,TIMES@GAME OVER" at 120,120
  260. Endif
  261. Else 
  262. Rem see if done exploding
  263. If GetSpriteFrame(boom) = 10 Then
  264. HideSprite boom
  265. StopSprite boom
  266. If GameOver = FALSE Then
  267. Rem reset player (if game not over)
  268. PlayerHit = FALSE
  269. SetSprite boom frame 0
  270. Else
  271. Rem End game when timer expires
  272. If Timer() - ExitTimer > 3500 Then RunGame = FALSE
  273. Endif
  274. Endif
  275. Endif
  276. Else
  277. Rem position the gun
  278. SetSprite gun to gunx,guny
  279. Endif
  280.  
  281. Rem update a fired missle
  282. If mislFired AND GameOver = FALSE then
  283. SetSprite misl to mislX,mislY
  284. mislY = mislY - mislSpeed
  285. if mislY < -20 then mislFired = FALSE
  286. Endif
  287.  
  288. Rem Display the score and lives
  289. DrawText "@16,BOLD,COURIER,BLACK@Score: @PURPLE@"+str$(score) at 2,220
  290. DrawText "@16,BOLD,COURIER,BLACK@Lives: @PURPLE@"+str$(Lives) at 230,220
  291.  
  292. return
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.